10. System-Level IO {CSAPP}
- 0015.1 CSAPP Third Edition Bryant, Randal E. O'Hallaron, David. ๐ป
- 07. Input Output {SP} | file descriptor, standard io,
read
,write
,open
,close
๋ฑ์ ๋ํ ๋ด์ฉ. - 11. memory mapping and Copy-on-write (COW) {SP} |
fork
์execve
, shared library๋ฅผ ์ฌ๋ฌ ํ๋ก์ธ์ค๊ฐ ๊ณต์ ํ๋ ๊ธฐ๋ฒ์ธ Copy on Write ๊ธฐ๋ฒ, ๋๋ง์mmap
- 11. Network Programming {CSAPP} ์ฑํฐ์์
Tiny
์น ์๋ฒ๋ฅผ ๋ง๋ค ๋ ํ์ํRIO
ํจํค์ง๊ฐ ์ด ์ฑํฐ์ ๋ค์ด์์.
README
์ด ํ์ผ์ ์ฌ์ค 11. Network Programming {CSAPP}, ์น ์๋ฒ ๋ง๋๋ ๋ฐ ํ์ํ RIO
ํจํค์ง ๋๋ฌธ์ ๋์ด์๋ค. ํ์ผ ๋์คํฌ๋ฆฝํฐ์ ์
๋ ฅ์ถ๋ ฅ์ ๋ํ ๋ด์ฉ์ ๋น ๋ฅด๊ฒ ์ง๊ณ ๋ฐ๋ก RIO
ํจํค์ง ๊ตฌํ์ฒด๋ก ๋์ด๊ฐ ๊ฒ. ๋์ถฉ ๋ดค๋๋ฐ ๋ฒํผ๊ฐ ์์ด ๋น ๋ฅด๋ค๊ณ ?
์ ๋์ค ์ธ์์์ ๋ชจ๋ ๊ฒ์ ํ์ผ์ด๋ค.
๋ํ์์ ๊ต์๋์ด ๊ท์ ๋ฑ์ง๊ฐ ์๋๋ก ๋ง์ํ์ จ๋ค. ๊ทธ๋์ ํ์ผ์ ์ด๋ป๊ฒ ๋ค๋ฃจ๋์ง, file descriptor๊ฐ ๋ฌด์์ธ์ง, ํ์ผ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ด๋ป๊ฒ ์ฐ๋์ง ๋ฑ๋ฑ์ ๋ํ ๋ด์ฉ์ 0015.2 Systems Programming {ssu2021-1st} ๐ผ ์์ ์๊ฐ์ ๋ค์๋ค.
mmap
์ ์ ๋์ค ์ธ์์์ ์ค์์ค ์๋ฏธ ๋์ดํ์ด๋ค.
์์ธํ ๊ฑด 11. memory mapping and Copy-on-write (COW) {SP}์์ ํ์ธ๋ฐ๋.
10.5. Robust Reading and Writing with the RIO
Package
๋ ๊ฐ์ง ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
-
Unbuffered Input & output: ์ ํ๋ฆฌ์ผ์ด์ ์์ค์ ๋ฒํผ ์์ด ๊ณง์ฅ fd๋ฅผ ์ฌ์ฉํด ํ์ผ์ ์ฝ๊ฑฐ๋ ์ธ ์ ์๋ค.
ssize_t rio_readn(int fd, void *usrbuf, size_t n);
ssize_t rio_writen(int fd, void *usrbuf, size_t n);
- Returns: number of bytes transferred if OK, 0 on EOF (rio_readn only), โ1 on error
-
Buffered Input: thread safeํ ๋ฒํผ๊ธฐ๋ฐ ์ ๋ ฅ์ ํตํด ์ค๋ฒํค๋๋ฅผ ์ค์ธ๋ค.
ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen);
ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n);
- Returns: number of bytes read if OK, 0 on EOF, โ1 on error
void rio_readinitb(rio_t *rp, int fd);
: rio ๊ฐ์ฒด ์ด๊ธฐํ, file descriptor ํ์.
10.6. Readig File Metadata
stat(2) ์ฐธ์กฐ
#include <unistd.h>
#include <sys/stat.h>
int stat(const char *filename, struct stat *buf);
int fstat(int fd, struct stat *buf);
// Returns: 0 if OK, โ1 on error
struct stat *
์ ํ์ผ ๋ฉํ๋ฐ์ดํฐ๊ฐ ๋ด๊ธด๋ค.
- Access permission bits defined in sys stat.h
- file types
S_ISREG(m)
: Is this a regular file?S_ISDIR(m)
: Is this a directory?S_ISSOCK(m)
: Is this a network socket?
/* Metadata returned by the stat and fstat functions */
struct stat {
dev_t st_dev; /* Device */
ino_t st_ino; /* inode */
mode_t st_mode; /* Protection and file type */
nlink_t st_nlink; /* Number of hard links */
uid_t st_uid; /* User ID of owner */
gid_t st_gid; /* Group ID of owner */
dev_t st_rdev; /* Device type (if inode device) */
off_t st_size; /* Total size, in bytes */
unsigned long st_blksize; /* Block size for filesystem I/O */
unsigned long st_blocks; /* Number of blocks allocated */
time_t st_atime; /* Time of last access */
time_t st_mtime; /* Time of last modification */
time_t st_ctime; /* Time of last change */
};
10.7. Read Directory Contents
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
// Returns: pointer to handle if OK, NULL on error
directory stream์ ์ฒซ๋ฒ์งธ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํจ๋ค. ํด๋น ๋๋ ํ ๋ฆฌ ์์๊ฐ ๊ณ ๊ฐ๋ ๋๊น์ง readdir
์ ํธ์ถํ๋ฉด ๋ค์ struct dirent *
ํฌ์ธํฐ๋ฅผ ์ป์ ์ ์๋ค.
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
// Returns: pointer to next directory entry if OK, NULL if no more entries or error
struct dirent
ino_t d_ino
: inode numberchar d_nme[256]
: filename
DIR *
๊ฐ์ฒด๋ฅผ ๋ซ์ ๊ฒฝ์ฐ closedir
์ ํธ์ถํ๋ฉด ๋๋ค.
10.8. Sharing Files
- Descriptor table: file descriptor ๋ชฉ๋ก, ํ๋ก์ธ์ค ๋ ๋ฆฝ์ ์.
- File table: ๋ชจ๋ ํ๋ก์ธ์ค๊ฐ ๊ณต์ ํ๋ ์ด๋ฆฐ ํ์ผ ๋ชฉ๋ก. reference counting์ ํ๋ค. 0์ด ๋ ๋๊น์ง file descriptor๋ฅผ ๋ซ์ผ๋ฉด ๋น๋ก์ ์ญ์ ๋๋ค.
- v-node table: ๋ชจ๋ ํ๋ก์ธ์ค๊ฐ ๊ณต์ ํ๋ ์ด๋ฆฐ ํ์ผ ๋ชฉ๋ก. File Table๊ณผ ๋ค๋ฅธ ์ ์ ์๋ ํ์ผ ๋ฉํ๋ฐ์ดํฐ์ธ
struct stat
์ ๊ฐ์ง๊ณ ์๋ค.
stack exchange ๋ต๋ณ์ ๋ฐ๋ฅด๋ฉด, ์ปค๋์ด ์ด ์ ์๋ ์ต๋ ํ์ผ์ ๊ฐ์๋ฅผ /proc/sys/fs/file-max
์ ์ ์ฅํด ๋๊ณ ์๋ค๊ณ ํ๋ค. ๋ด ์ปดํจํฐ๋ 2459860๊ฐ๊ฐ ๋๋๋ฐ, ์ ์ ํ๋ก์ธ์ค ๋น file descriptor์ ๊ฐ์๋ ๋ฑํ ์ ํด์ ธ ์์ง ์์๊ฒ, file descriptor๋ ๊ฐ์ ํ์ผ(v-node)์ ๊ฐ๋ฆฌํฌ ์๋ ์๊ณ , ๊ฐ์ ํ์ผ ํ
์ด๋ธ(file table)์ ๊ฐ๋ฆฌํฌ ์๋ ์์ผ๋ฉฐ, ์์ ๊ฐ๋ฆฌํค์ง ์์ ์๋ ์์ด์๋๋ค.
I/O Redirection
์์ฃผ ์ค์ํ ๊ฐ๋
์ธ dup2์ ๋ํด์ ๋์จ๋ค. ๋ฆฌ๋
์ค ์
ธ์์๋ >
, 2>
, <
๊ฐ ์๋ค. ๊ฐ๊ฐ
lhs > rhs
:lhs
์ stdout ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผrhs
ํ์ผ๋ก ๋ฆฌ๋๋ ์ ํ์์ค.lhs 2> rhs
:lhs
์ stderr ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผrhs
ํ์ผ๋ก ๋ฆฌ๋๋ ์ ํ์์ค.lhs < rhs
:rhs
ํ์ผ์lhs
์ stdin์ผ๋ก ๋ฆฌ๋๋ ์ ํ์์ค.
#include <unistd.h>
int dup2(int oldfd, int newfd);
// Returns: nonnegative descriptor if OK, โ1 on error
descriptor table์ newfd
๋ฒ์งธ ์ธ๋ฑ์ค์ ๊ฐ์ oldfd
์ ๊ฐ์ผ๋ก ๋ฎ์ด์ด๋ค. ๋ง์ฝ newfd
๊ฐ ์ด๋ ค์๋ค๋ฉด ์๋์ผ๋ก ๋ซ๋๋ค.